﻿libModule.factory("notification", ["$rootScope", function ($rootScope) {
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "positionClass": "toast-bottom-left",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "500",
        "timeOut": "2000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };

    function handleResponse(data) {
        try {
            if (data.Success)
                success(data);
            else
                error(data);            
        } catch (e) {
            error(e);
        }
    };

    function success(data) {        
        toastr.success('Success');
    };

    function info(message) {
        toastr.info(message);
    }

    function error(e) {        
        toastr.warning(e.message);
        console.error(e);       
    };

    function notify(value) {
        if (value instanceof Error) {
            error(value);
            return;
        }

        if (typeof (value) == "object") {
            handleResponse(value);
            return;
        }

        if (typeof (value) == "string")
            info(value);
    };

    return {
            notify: notify
        }    
}]);